import pymysql

from conn_mysql import ConnectMysql

class ApiSearch(ConnectMysql):


    def need_his(self,keyword):

        sql = f'select * from need_his where need like "%{keyword}%"'

        result_info = self.select_all(sql)

        return result_info


    def search_need_api(self, methods, keyword):

        if not methods == 'GET':
        
            return {
                'status': -1,
                'errorCode': 4,
                'errorMsg':f'请检查request.method:{methods}请重新请求',
                'resultBody':None
            }

        if not keyword:

            return {
                    'status': -1,
                    'errorCode': 1,
                    'errorMsg':f'请检查参数是否为空,keyword:{keyword},请重新输入',
                    'resultBody':None
                }

        result_info = self.need_his(keyword)

        if result_info:

            return {
                    'status': 1,
                    'errorCode': None,
                    'errorMsg':None,
                    'resultBody':result_info
                }

        else:

            return {
                    'status': -1,
                    'errorCode': 2,
                    'errorMsg':f'没有找到你想要查询的需求',
                    'resultBody':None
                }

    
        

